/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.cx1.dao.api.IEmailInviteeDao;
import org.cx1.dao.api.IMobileInviteeDao;
import org.cx1.dao.impl.EmailInviteeDao;
import org.cx1.dao.impl.MobileInviteeDao;
import org.cx1.model.Invite;
import org.cx1.model.User;
import org.cx1.model.ds.MockDS;
import org.cx1.model.utils.EmailInvitee;
import org.cx1.model.utils.Invitee;
import org.cx1.model.utils.MobileInvitee;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.service.api.IInviteService;
import org.cx1.service.api.IUserService;

/**
 *
 * @author ECH
 */
public class DInviteService implements IInviteService {

    private IEmailInviteeDao emailInviteeDao = new EmailInviteeDao();
    private IMobileInviteeDao mobileInviteeDao = new MobileInviteeDao();
    
    private IUserService userService = new DUserService();
    
    @Override
    public ServiceResponse<String> create(String name, String email, String type, String area) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<List<Invite>> readll(int offset, int limit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<List<Invitee>> readAllInvitee(int offset, int limit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<String> createInvitee(Invitee invitee) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> deleteInvitee(String id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<List<EmailInvitee>> readAllEmailInvitee(int offset, int limit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<String> createEmailInvitee(EmailInvitee emailInvitee) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String email = null;        
        try {  
            if (emailInvitee.getEmail() != null && !StringUtils.isEmpty(emailInvitee.getEmail().trim())) {
                EmailInvitee entry = new EmailInvitee();
                
                entry.setName(emailInvitee.getName());
                entry.setEmail(emailInvitee.getEmail().trim());

                email = this.emailInviteeDao.add(entry);
                
                System.out.println("CX: " + email);
                
                response.setResponse(email);     
                response.setSuccessful(true); 
            }      
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<Boolean> deleteEmailInvitee(String id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> deleteEmailInviteeByEmail(String email) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            this.emailInviteeDao.deleteByEmail(email);
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteExisitingUsersInEmailInviteList() {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            ServiceResponse<List<User>> response1 = userService.readAll(0, 0);            
            if (response1.isSuccessful() && response1.getResponse() != null && response1.getResponse().size() > 0) {
                for (User user : response1.getResponse()) {
                    this.deleteEmailInviteeByEmail(user.getEmail());
                }
            }
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteDuplicatesInEmailInviteList() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<List<EmailInvitee>> readEmailInviteesByProcessed(int offset, int limit, boolean processed) {
        ServiceResponse<List<EmailInvitee>> response = new ServiceResponse<List<EmailInvitee>>();
        List<EmailInvitee> emailInvitees = null;
        try {            
            emailInvitees = this.emailInviteeDao.getAll(processed, offset, limit);
            response.setResponse(emailInvitees);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> setEmailInviteeProcessed(String email, boolean processed) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                        
            this.emailInviteeDao.setProcessed(email, processed);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }

    @Override
    public ServiceResponse<List<MobileInvitee>> readAllMobileInvitee(int offset, int limit) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<String> createMobileInvitee(MobileInvitee mobileInvitee) {
        ServiceResponse<String> response = new ServiceResponse<String>();
        String mobile = null;        
        try {  
            if (mobileInvitee.getMobile() != null && !StringUtils.isEmpty(mobileInvitee.getMobile().trim())) {
                MobileInvitee entry = new MobileInvitee();
                
                entry.setName(mobileInvitee.getName());
                entry.setMobile(mobileInvitee.getMobile().trim());

                mobile = this.mobileInviteeDao.add(entry);
                
                System.out.println("CX: " + mobile);
                
                response.setResponse(mobile);     
                response.setSuccessful(true); 
            }      
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> deleteMobileInvitee(String id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public ServiceResponse<Boolean> deleteMobileInviteeByMobile(String mobile) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {            
            this.mobileInviteeDao.deleteByMobile(mobile);
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<List<MobileInvitee>> readMobileInviteesByProcessed(int offset, int limit, boolean processed) {
        ServiceResponse<List<MobileInvitee>> response = new ServiceResponse<List<MobileInvitee>>();
        List<MobileInvitee> mobileInvitees = null;
        try {            
            mobileInvitees = this.mobileInviteeDao.getAll(processed, offset, limit);
            response.setResponse(mobileInvitees);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response;
    }

    @Override
    public ServiceResponse<Boolean> setMobileInviteeProcessed(String mobile, boolean processed) {
        ServiceResponse<Boolean> response = new ServiceResponse<Boolean>();
        boolean success = false;
        try {                        
            this.mobileInviteeDao.setProcessed(mobile, processed);
            success = true;
            response.setResponse(success);
            response.setSuccessful(true);            
        } 
        catch (Exception ex) {
            response.setResponse(null);
            response.setSuccessful(false);
            response.setMessage(ex.getMessage());
            response.setException(ex);
            ex.printStackTrace();            
        }
        return response; 
    }
    
}
